#include <stdlib.h>
#include <goocanvas.h>
#include <gdk/gdkkeysyms.h>

#define REGISTER_TEXT         "注      册"
#define LOGIN_TEXT            "登      陆"
#define NETWORK_SETUP_TEXT    "网络设置"

GdkPixbuf *button_fg, *button_bg;
GtkWidget *entry_user, *entry_passwd;
GtkWidget *checkbtn_remeber_info, *checkbtn_auto_login;

static gboolean on_rect_button_press (GooCanvasItem  *view,
				      GooCanvasItem  *target,
				      GdkEventButton *event,
				      gpointer        data);

static gboolean on_delete_event      (GtkWidget      *window,
				      GdkEvent       *event,
				      gpointer        unused_data);

static gboolean
on_focus_in (GooCanvasItem *item,
	     GooCanvasItem *target,
	     GdkEventFocus *event,
	     gpointer data)
{
  gchar *id;

  id = g_object_get_data (G_OBJECT (item), "id");

  g_print ("%s received focus-in event\n", id ? id : "unknown");

  /* Note that this is only for testing. Setting item properties to indicate
     focus isn't a good idea for real apps, as there may be multiple views. */
  //g_object_set (item, "stroke-color", "black", NULL);
  g_object_set (item, "pixbuf", button_fg, NULL);
  return FALSE;
}


static gboolean
on_focus_out (GooCanvasItem *item,
	      GooCanvasItem *target,
	      GdkEventFocus *event,
	      gpointer data)
{
  gchar *id;

  id = g_object_get_data (G_OBJECT (item), "id");

  g_print ("%s received focus-out event\n", id ? id : "unknown");

  /* Note that this is only for testing. Setting item properties to indicate
     focus isn't a good idea for real apps, as there may be multiple views. */
  //g_object_set (item, "stroke-pattern", NULL, NULL);
  g_object_set (item, "pixbuf", button_bg, 	NULL);
  return FALSE;
}


static gboolean
on_button_press (GooCanvasItem *item,
		 GooCanvasItem *target,
		 GdkEventButton *event,
		 gpointer data)
{
  GooCanvas *canvas;
  gchar *id;

  id = g_object_get_data (G_OBJECT (item), "id");

  g_print ("%s received button-press event\n", id ? id : "unknown");

  canvas = goo_canvas_item_get_canvas (item);
  goo_canvas_grab_focus (canvas, item);

  return TRUE;
}


static gboolean
on_key_press (GooCanvasItem *item,
	      GooCanvasItem *target,
	      GdkEventKey *event,
	      gpointer data)
{
  gchar *id;

  id = g_object_get_data (G_OBJECT (item), "id");
  g_print ("%s received key-press event\n", id ? id : "unknown");

  printf("~~~%s\n",__FUNCTION__);
  GdkEventKey*	key = ( GdkEventKey* ) event;
  switch (key->keyval)
  {
     case GDK_Return:
    	 if (g_str_equal (id, LOGIN_TEXT))
    	 {
    		 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbtn_remeber_info)))
    		 {
    			 g_print ("remeber info is check!\n");
    		 }
    		 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbtn_auto_login)))
    		 {
    			 g_print ("auto login is check!\n");
    		 }

    		 g_print ("user textfield: %s\n", gtk_entry_get_text(GTK_ENTRY (entry_user)));
    		 g_print ("passwd textfield: %s\n", gtk_entry_get_text(GTK_ENTRY (entry_passwd)));
    	 } else if (g_str_equal (id, REGISTER_TEXT))
    	 {
    		 g_print ("PRESS BUTTON LOGIN.\n");
    	 } else if (g_str_equal (id, NETWORK_SETUP_TEXT))
    	 {
    		 g_print ("PRESS BUTTON NETWORK_SETUP.\n");
    		 //execl ("/usr/local/bin/network-setup", "network-setup", "lan", (char *)0);
    	 }
    	 break;
  }


  return FALSE;
}

void setup_signal(GooCanvasItem *item)
{
	g_signal_connect (item, "focus_in_event",
			    G_CALLBACK (on_focus_in), NULL);
	g_signal_connect (item, "focus_out_event",
			    G_CALLBACK (on_focus_out), NULL);

	g_signal_connect (item, "button_press_event",
			    G_CALLBACK (on_button_press), NULL);

	g_signal_connect (item, "key_press_event",
			    G_CALLBACK (on_key_press), NULL);
}

static void
create_focus_box (GtkWidget     *canvas,
		          gdouble        x,
		          gdouble        y,
		          GdkPixbuf     *pixbuf,
		          gchar         *name)
{
  GooCanvasItem *root, *item;
  gchar font[64];

  sprintf(font ,"Sans Bold %d", 18);

  root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
  item = goo_canvas_image_new (root, pixbuf, x, y,
		                       "can-focus", TRUE,
		                       NULL);

  goo_canvas_text_new (root, name, x+20, y+18, -1, GTK_ANCHOR_NW,
  		               "font", font,
  					   NULL);

  g_object_set_data (G_OBJECT (item), "id", name);
  setup_signal (item);
}

static void create_checkbox (GtkWidget     *canvas,
		                     GtkWidget     *checkbtn,
                             gdouble        x,
                             gdouble        y,
                             gchar         *name)
{
	GooCanvasItem *root, *item;
	root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
	//g_object_set (checkbtn, "indicator-size", 15, NULL);
	item = goo_canvas_widget_new (root, checkbtn, x, y, -1, -1,
			                      "can-focus", TRUE,
			                      NULL);
	setup_signal (item);
}

/*static void create_textfield (GtkWidget     *canvas,
		                      gdouble       x,
		                      gdouble       y,
		                      gdouble       width,
		                      gdouble       height,
		                      gchar         *char)
{
	//entry = gtk_entry_new ();
}*/

int
main (int argc, char *argv[])
{
  GtkWidget *window, *canvas;
  //GtkWidget *entry;

  GooCanvasItem *name_label, *passwd_label;
  GooCanvasItem *root, *item, *text_item;
  GooCanvasItem *name_field_item, *passwd_field_item;

  GdkPixbuf *pixbuf;

//  char font[64];

  /* Initialize GTK+. */
  gtk_set_locale ();
  gtk_init (&argc, &argv);

  /* Create the window and widgets. */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 800, 480);
  gtk_widget_show (window);
  g_signal_connect (window, "delete_event", G_CALLBACK (on_delete_event),
		    NULL);

  pixbuf = gdk_pixbuf_new_from_file ("Login-bg.png", NULL);
  button_fg = gdk_pixbuf_new_from_file ("button-fg.png", NULL);
  button_bg = gdk_pixbuf_new_from_file ("button-bg.png", NULL);

  /* init the canvas */
  canvas = goo_canvas_new ();
  GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
  root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
  gtk_widget_set_size_request (canvas, 800, 480);
  goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 800, 480);
  gtk_container_add (GTK_CONTAINER (window), canvas);
  gtk_widget_show (canvas);

  /* print the background pic on the canvas */
  item = goo_canvas_image_new (root, pixbuf, 0, 0, NULL);

  /*sprintf(font ,"Sans Bold %d", 20);
  name_label = goo_canvas_text_new (root, "姓名：", 266, 109, -1, GTK_ANCHOR_NW,
		                            "font", font,
									NULL);
  passwd_label = goo_canvas_text_new (root, "密码：", 266, 163, -1, GTK_ANCHOR_NW, 
		                              "font", font,
									  NULL);*/

  entry_user = gtk_entry_new ();
  name_field_item = goo_canvas_widget_new (root, entry_user, 359, 109, 200, 50, NULL);
  g_object_set_data (G_OBJECT (name_field_item), "id", "name_field_entry");

  entry_passwd = gtk_entry_new ();
  gtk_entry_set_invisible_char (GTK_ENTRY(entry_passwd), '*');
  gtk_entry_set_visibility (GTK_ENTRY(entry_passwd), FALSE);
  passwd_field_item = goo_canvas_widget_new (root, entry_passwd, 359, 163, 200, 50, NULL);
  g_object_set_data (G_OBJECT (passwd_field_item), "id", "passwd_field_entry");

  create_focus_box (canvas, 173, 306, button_bg, REGISTER_TEXT);
  create_focus_box (canvas, 332, 306, button_bg, LOGIN_TEXT);
  create_focus_box (canvas, 489, 306, button_bg, NETWORK_SETUP_TEXT);

  checkbtn_remeber_info = gtk_check_button_new ();
  checkbtn_auto_login = gtk_check_button_new ();
  create_checkbox (canvas, checkbtn_remeber_info, 304, 221, "remeber_info");
  create_checkbox (canvas, checkbtn_auto_login, 304, 260, "auto_login");

  /* Pass control to the GTK+ main event loop. */
  gtk_main ();

  return 0;
}

/* This is our handler for the "delete-event" signal of the window, which
   is emitted when the 'x' close button is clicked. We just exit here. */
static gboolean
on_delete_event (GtkWidget *window,
		 GdkEvent  *event,
		 gpointer   unused_data)
{
  exit (0);
}
